//
//  DestinationViewController.m
//  QYer
//
//  Created by lanou on 16/8/5.
//  Copyright © 2016年 CaiZhuYu. All rights reserved.
//

#import "DestinationViewController.h"
#import "CollectionReusableView.h"
#import "DestinationModel.h"
#import "MyCollectionViewCell.h"
#import "CountryCollectionViewCell.h"
#import "MapCollectionReusableView.h"
#import "EmptyCollectionReusableView.h"
#import "CollectionReusableView.h"
#import "DestinationDetailViewController.h"

@interface DestinationViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) MapCollectionReusableView *mapV;

@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray  *hotArray;
@property (nonatomic, strong) NSString *hotTitle;
@end

@implementation DestinationViewController

// 懒加载属性数组
- (NSMutableArray *)dataArray
{
    if(!_dataArray)
    {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (NSMutableArray *)hotArray
{
    if(!_hotArray)
    {
        _hotArray = [[NSMutableArray alloc] init];
    }
    return _hotArray;
}






// collectionView布局
- (void)createFlowLayout
{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // item大小
    _flowLayout.itemSize = CGSizeMake((kScreenWidth - 45) / 2, 300);
    // 最小列间距
    _flowLayout.minimumInteritemSpacing = 45 / 3;
    // 最小行间距
    _flowLayout.minimumLineSpacing = 15;
    // 分区内容边间距
    _flowLayout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
    // 头视图尺寸
    _flowLayout.headerReferenceSize = CGSizeMake(0, 35);
    // 滑动方向
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
}


// 创建collectionView
- (void)createCollectionView
{
    // 初始化collectionView
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) collectionViewLayout:_flowLayout];
    
    // 设置代理
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    
    // 设置背景颜色
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    // 注册cell/item
    [_collectionView registerNib:[UINib nibWithNibName:@"MyCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    [_collectionView registerNib:[UINib nibWithNibName:@"CountryCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"countryCell"];
    
    // 注册重用头视图
    [_collectionView registerNib:[UINib nibWithNibName:@"CollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    [_collectionView registerNib:[UINib nibWithNibName:@"EmptyCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"EmptyView"];
    
    // 添加视图
    [self.view addSubview:_collectionView];
}


// 数据解析
- (void)dataAnalysis:(NSInteger)tag
{
    
    [NetWorkTool requstWithType:GET URLString: kDestinationUrl parameter:nil callBack:^(NSData *data, NSError *error)
    {
        [_dataArray removeAllObjects];
        [_hotArray removeAllObjects];
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        
        
        
        NSArray *dataArray = dic[@"data"][tag - 100][@"country"];
        
        for (NSDictionary* DataDic in dataArray)
        {
            DestinationModel *model = [[DestinationModel alloc] init];
            [model setValuesForKeysWithDictionary:DataDic];
            // 获取其他城市的信息
            [_dataArray addObject:model];
        }
        
        NSArray *dataArray2 = dic[@"data"][tag - 100][@"hot_country"];
        for (NSDictionary *DataDic2 in dataArray2)
        {
            DestinationModel *model = [[DestinationModel alloc] init];
            [model setValuesForKeysWithDictionary:DataDic2];
            // 获取热门城市的信息
            [_hotArray addObject:model];
        }
        
        self.hotTitle = dic[@"data"][tag - 100][@"cnname"];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
        });
        
    }];
}



- (void)getData:(UIButton *)button
{
    NSLog(@"%ld", button.tag);
    
    for (NSInteger i = 100; i < 107; i++)
    {
        // 通过新建一个按钮找到想要的按钮
        UIButton *btn = [self.mapV viewWithTag:i];
        if (btn.tag == button.tag)
        {
            // 如果是选中状态, 改变按钮的字体颜色
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            // 调用数据解析的方法
            [self dataAnalysis:btn.tag];
        }
        else
        {
            // 如果不在选中状态, 则取消字体颜色的变化
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
}



// 每次加载视图前走的方法
- (void)viewWillAppear:(BOOL)animated {
    [self dataAnalysis:100];
    [self.mapV.AsiaLabel setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 
    self.mapV = [[MapCollectionReusableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 240)];
    self.mapV = [MapCollectionReusableView mapCollectionReusableView];
    
    [self.mapV.AsiaLabel addTarget:self action:@selector(getData:) forControlEvents:UIControlEventTouchUpInside];

    [self.mapV.EuropeLabel addTarget:self action:@selector(getData:) forControlEvents:UIControlEventTouchUpInside];

    [self.mapV.NorthLabel addTarget:self action:@selector(getData:) forControlEvents:UIControlEventTouchUpInside];
  
    [self.mapV.SouthLabel addTarget:self action:@selector(getData:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.mapV.OceaniaLabel addTarget:self action:@selector(getData:) forControlEvents:UIControlEventTouchUpInside];
   
    [self.mapV.AfricaLabel addTarget:self action:@selector(getData:) forControlEvents:UIControlEventTouchUpInside];
   
    [self.mapV.Antarctica addTarget:self action:@selector(getData:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self createFlowLayout];
    
    [self createCollectionView];
    
    
    
}






- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return self.hotArray.count;
    }
    else
    {
        return self.dataArray.count;
    }
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0)
    {
        MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        DestinationModel *model = self.hotArray[indexPath.row];
        [cell setValueWithModel:model];
        return cell;
    }
    else
    {
        CountryCollectionViewCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"countryCell" forIndexPath:indexPath];
        DestinationModel *model = self.dataArray[indexPath.row];
        [myCell setValueWithModel:model];
        return myCell;
    }
}



- (CGSize)collectionView:(MyCollectionViewCell *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return CGSizeMake((kScreenWidth - 45) / 2, 240);
    }
    return CGSizeMake((kScreenWidth - 45) / 2, 70);
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(15, 15, 15, 15);
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"第%ld分区, 第%ld个item", indexPath.section, indexPath.row);
    if (indexPath.section == 0)
    {
        DestinationModel *model = self.hotArray[indexPath.row];
        DestinationDetailViewController *detailVC = [[DestinationDetailViewController alloc] init];
        detailVC.ID = model.myID;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
    else
    {
        DestinationModel *model = self.dataArray[indexPath.row];
        DestinationDetailViewController *detailVC = [[DestinationDetailViewController alloc] init];
        detailVC.ID = model.myID;
        [self.navigationController pushViewController:detailVC animated:YES];    }
    
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        if(indexPath.section == 0)
        {
            EmptyCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"EmptyView" forIndexPath:indexPath];
            
            [headerView addSubview:self.mapV];
            
            self.mapV.titleLabel.text = [NSString stringWithFormat:@"%@热门目的地", self.hotTitle];
            return headerView;
        }
        
        CollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        headerView.titleLabel.text = [NSString stringWithFormat:@"%@其他目的地", self.hotTitle];
        
        return headerView;
    }
    else
    {
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
        
        footerView.backgroundColor = [UIColor greenColor];
         
        return footerView;
    }

}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if(section == 0)
    {
        return CGSizeMake(kScreenWidth, 240);
    }
    return CGSizeMake(kScreenWidth, 40);
}





























- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
